import BaseObj from "../BaseObj";
import Time from "../Time";

export default class Circle extends BaseObj{
    radius:number;
    speed:number;
    direction:number[] = [0,0]
    id:number
    constructor(id:number){
        super()
        this.id = id
        this.x = Math.random()*window.innerWidth
        this.y = Math.random()*window.innerHeight
        this.radius = 10+Math.random()*20
        this.speed = 120+Math.random()*30
        //左右
        this.direction[0] =-1+ Math.random()*2
        //上下
        this.direction[1] = -1+ Math.random()*2
    }
    draw(ctx: CanvasRenderingContext2D) {
        
        ctx.strokeStyle = "blue"
        ctx.arc(this.x,this.y,this.radius,0,360)
        ctx.stroke()

        ctx.strokeStyle = "blue"
        ctx.textBaseline = "middle"
        ctx.strokeText(this.id+"",this.x-5,this.y)
    }

    update(): void {
        this.x += this.direction[0]*this.speed*Time.frameTime
        this.y += this.direction[1]*this.speed*Time.frameTime
        if((this.x-this.radius)<0 || (this.x+this.radius)>window.innerWidth){
            this.direction[0] = -this.direction[0]
        }
        if((this.y-this.radius)<0 || (this.y+this.radius)>window.innerHeight){
            this.direction[1] = -this.direction[1]
        }
    }

    containPoint(x:number,y:number){
        return Math.pow((x-this.x),2)+Math.pow((y-this.y),2) < Math.pow(this.radius,2)
    }

}